package com.mtw.bbs.fileAdmin.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtw.bbs.common.redisBase.util.RedisUtilz;
import com.mtw.bbs.fileAdmin.mapper.VideoInfoMapper;
import com.mtw.bbs.fileAdmin.pojo.entity.VideoInfo;
import com.mtw.bbs.fileAdmin.pojo.vo.VideoInfoVo;
import com.mtw.bbs.fileAdmin.service.VideoInfoService;
import com.mtw.bbs.fileAdmin.strategy.UploadStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.mtw.bbs.fileAdmin.constant.FAConstants.COVER_URL_REDIS_KEY;


/**
 * 逻辑层
 */
@Slf4j
@Service("videoInfoService")
@RequiredArgsConstructor
public class VideoInfoServiceImpl extends ServiceImpl<VideoInfoMapper, VideoInfo> implements VideoInfoService {

    /**
     * 上传策略
     */
    @Value("${strategy.upload}")
    private String strategy;


    private final RedisUtilz<String,VideoInfoVo> redisUtilz;
    private final Collection<UploadStrategy> uploadStrategies;




    /**
     * 获取视频信息
     * @param id    视频文件id
     */
    @Override
    public VideoInfoVo getInfoByI(String id) {

        if (StringUtils.isBlank(id)){
            return new VideoInfoVo();
        }

        VideoInfoVo vo = redisUtilz.get(COVER_URL_REDIS_KEY + id);
        if (vo != null){
            return vo;
        }

        VideoInfo file = getById(id);
        if (file == null){
            return new VideoInfoVo();
        }
        vo = new VideoInfoVo();
        vo.setId(id);
        vo.setVideoDuration(file.getVideoDuration());


        for (UploadStrategy item : uploadStrategies) {
            if (item.strategy().equals(strategy)){
                String fileUrl = item.getFileUrl(file.getCoverPath());
                vo.setCover(fileUrl);
                redisUtilz.setForTimeMIN(COVER_URL_REDIS_KEY + id,vo,23*60);
            }
        }
        return vo;
    }




    /**
     * 批量获取视频信息
     * @param ids    视频文件id集合
     */
    @Override
    public List<VideoInfoVo> getInfoByIs(Collection<String> ids) {

        if (CollectionUtils.isEmpty(ids)){
            return new ArrayList<>(0);
        }
        List<VideoInfoVo> result = new ArrayList<>(ids.size());
        Set<String> needDatebase = new HashSet<>(ids.size());

        for (String id : ids) {
            if (StringUtils.isBlank(id)){
                continue;
            }
            // 查redis
            VideoInfoVo vo = redisUtilz.get(COVER_URL_REDIS_KEY + id);
            if (vo != null){
                result.add(vo);
            }else {
                needDatebase.add(id);
            }
        }

        // 缓存不存在
        if (!needDatebase.isEmpty()){
            for (VideoInfo file : listByIds(needDatebase)) {
                VideoInfoVo nvo = new VideoInfoVo();
                nvo.setId(file.getId());
                nvo.setVideoDuration(file.getVideoDuration());
                for (UploadStrategy item : uploadStrategies) {
                    if (item.strategy().equals(strategy)){
                        String fileUrl = item.getFileUrl(file.getCoverPath());
                        nvo.setCover(fileUrl);
                        redisUtilz.setForTimeMIN(COVER_URL_REDIS_KEY + file.getId(),nvo,23*60);
                        result.add(nvo);
                    }
                }
            }

        }

        return result;
    }
}
